﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper.Internal.Mappers;
using BW.WMS.BasicData_LC.Dto.Input;
using BW.WMS.BasicData_LC.Dto.OutPut;
using BW.WMS.CodeEnum;
using BW.WMS.LC.Dto.Input;
using BW.WMS.Model;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace BW.WMS.LC
{
    public class CodeAppService : ApplicationService, ICodeAppService
    {
        private readonly IRepository<Dm_Code, Guid> _codeRepository;

        public CodeAppService(IRepository<Dm_Code, Guid> codeRepository)
        {
            _codeRepository = codeRepository;
        }
        /// <summary>
        /// 入库类型添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> CreateCode(CreateUpdateCodeDtoInput input)
        {

            var result=new ApiResult<int>();

            result.Code = ApiEnum.Success;
            result.Message = "添加成功";
            input.Id=Guid.NewGuid();
            var code = ObjectMapper.Map<CreateUpdateCodeDtoInput, Dm_Code>(input);

          var a= await _codeRepository.InsertAsync(code);

            result.Data = 1;
            if (a == null)
            {
                result.Code = ApiEnum.Error;
                result.Message = "添加失败";
                result.Data = -1;
            }

            return result;
        }
        /// <summary>
        /// 入库类型删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteCode(Guid id)
        {
            var result = new ApiResult<int>();
            result.Code = ApiEnum.Success;
            result.Message = "删除成功";
            var list = await _codeRepository.GetAsync(id);

            _codeRepository.DeleteAsync(list);

            result.Data = 1;

            if (list == null)
            {
                result.Code = ApiEnum.Error;
                result.Message = "删除失败";
                result.Data = -1;
            }

            return result;
        }

        /// <summary>
        /// 入库类型列表显示
        /// </summary>
        /// <param name="output"></param>
        /// <returns></returns>
        public async Task<ApiPaging<CodeDto>> GetCodeList(CodeDtoOutput output)
        {
            

            var query=await _codeRepository.GetQueryableAsync();

            if (!string.IsNullOrWhiteSpace(output.Typename))
            {
                query = query.Where(x => x.Typename.Contains(output.Typename));
            }

            if (!string.IsNullOrWhiteSpace(output.Typeid))
            {
                query = query.Where(a => a.Typeid.Contains(output.Typeid));
            }

            var totalcount = query.Count();

            var list=query.OrderBy(a=>a.CreationTime).Skip((output.pageindex - 1) * output.pagesize).Take(output.pagesize).ToList();

            var dto = ObjectMapper.Map<List<Dm_Code>, List<CodeDto>>(list);

            var result = new ApiPaging<CodeDto>();
            result.Code = ApiEnum.Success;
            result.Message = "获取成功";
            result.PageData=dto;
            result.TotalCount = totalcount;
            return result;
        }

        /// <summary>
        /// 入库修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateCode(CreateUpdateCodeDtoInput input)
        {
            var result = new ApiResult<int>();

            result.Code = ApiEnum.Success;
            result.Message = "修改成功";
            var code = ObjectMapper.Map<CreateUpdateCodeDtoInput, Dm_Code>(input);

            var a = await _codeRepository.UpdateAsync(code);

            result.Data = 1;
            if (a == null)
            {
                result.Code = ApiEnum.Error;
                result.Message = "修改失败";
                result.Data = -1;
            }

            return result;
        }
    }
}
